package com.lovo.test;

import java.io.IOException;
import java.util.Scanner;

public class Test {
    public static int[] randomArr = new int[4];//机选数组
    public static int[] userRandomArr = new int[4];//用户输入数数组
    public static int max = 9;//随机数最大值
    public static int min = 0;//随机数最小值
public static int temp=5;
    public static void main(String[] args) {
        random();

        //用户输入的数
        while (true) {
             nuesNumber();
            int a = compare();
            if (a == 4){
                System.out.println("你说的对");
                break;
            }
            if (temp ==0){
                System.out.println("次数用完了");
                break;
            }
            System.out.println("你还有"+temp+"次机会");
        }

    }
    public static void random(){
        //随机数赋值机选数组操作并且判断不相等
        for (int i = 0; i < randomArr.length; i++) {
            boolean fage = true;
            int random = (int) (Math.random() * (max - min + 1) + min);
            for (int j = 0; j < randomArr.length; j++) {
                if (randomArr[j] == random) {
                    fage = false;
                    i--;
                }

            }
            if (fage) {
                randomArr[i] = random;
            }
        }

        for (int item : randomArr) {
            System.out.println("机选数为" + item);
        }
    }

    public static void nuesNumber() {
        for (int f = 0; f < userRandomArr.length; f++) {
            boolean o = true;
            while (true) {
                try {
                    Scanner scan = new Scanner(System.in);
                    System.out.println("请输入第"+(f+1)+"数字");
                    int number = scan.nextInt();
                    for (int j = 0; j < f; j++) {
                        if (userRandomArr[j] == number) {
                            System.out.println("你输入的数重复，请重新输入");
                            f--;
                            o = false;
                        }
                    }
                    if (o) {
                        userRandomArr[f] = number;
                    }

                    break;

                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        temp--;
    }

        public static int compare () {
            int a = 0;
            int b = 0;
            for (int i = 0; i < userRandomArr.length; i++) {

                if (randomArr[i] == userRandomArr[i]) {
                    a++;
                } else {
                    for (int j = 0; j < randomArr.length; j++) {
                        if (randomArr[i] == userRandomArr[j]) {
                            b++;
                        }
                    }
                }
            }


            System.out.println(a + "A" + b + "B");
return a;
        }



}
